import argparse
import os
import shutil


def s2d(src_dir, dst_dir):
    if not os.path.exists(src_dir):
        raise Exception('目录不存在  ==>  {}'.format(src_dir))
    if not os.path.exists(dst_dir):
        os.makedirs(dst_dir)
    else:
        raise Exception('目录已存在  ==>  {}'.format(dst_dir))

    for root, dirs, files in os.walk(src_dir):
        for file in files:
            if file.startswith('__') or not file.endswith('.py'):
                continue
            src_file = os.path.join(root, file)
            relative_path = os.path.relpath(src_file, src_dir)
            dst_file = os.path.join(dst_dir, relative_path)
            os.makedirs(os.path.dirname(dst_file), exist_ok=True)
            shutil.copy2(src_file, dst_file)


def main():
    print('creating...')
    spider = argparse.ArgumentParser()
    spider.add_argument("-p", "--project", help="新建项目，spd new -p <project_name>")
    some = spider.parse_args()

    current_dir = os.path.abspath(os.getcwd())
    project_dir = '{}/{}'.format(current_dir, some.project)
    temp_dir = '{}/templates'.format(os.path.dirname(os.path.dirname(__file__)))
    s2d(temp_dir, project_dir)
    print('SUCCESSS  ==>  {}'.format(some.project))
